//
//  SelectProduceTypeViewController.m
//  MagictoLane
//
//  Created by 锋 on 2018/1/2.
//  Copyright © 2018年 HUAYU. All rights reserved.
//

#import "SelectProduceTypeViewController.h"

@interface SelectProduceTypeViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger page;
}
@property (nonatomic, strong) UITableView *addMachineTableView;
@property (nonatomic, strong) NSMutableArray *produceTypeArr;
@end

@implementation SelectProduceTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 0;
    [self setupUI];
    [self setupAddMachineTableView];
    [self requestProduceType];
    // Do any additional setup after loading the view.
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"工厂类型";
    self.view.backgroundColor = RGB(242, 242, 242);
}

- (void)setupAddMachineTableView {
    _addMachineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _addMachineTableView.scrollEnabled = NO;
    _addMachineTableView.separatorColor = RGB(242, 242, 242);
    _addMachineTableView.delegate = self;
    _addMachineTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _addMachineTableView.tableHeaderView = view;
    _addMachineTableView.tableFooterView = view;
    _addMachineTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_addMachineTableView];
}

- (void)requestProduceProduct:(NSString *)categoryId {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProduceProductParmers:@{@"category.id" : categoryId} Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             [_produceTypeArr removeAllObjects];
             _produceTypeArr = [SelectProduceTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             if (_addMachineTableView) {
                 CGFloat height = _produceTypeArr.count * 44;
                 if (height > SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) {
                     height = SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX;
                     _addMachineTableView.scrollEnabled = YES;
                 } else {
                     _addMachineTableView.scrollEnabled = NO;
                 }
                 _addMachineTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
             }
             self.navigationItem.title = @"生产产品";
             [_addMachineTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)requestProduceType {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProduceCategoryParmers:nil Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             [_produceTypeArr removeAllObjects];
             _produceTypeArr = [AddMachineModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             if (_addMachineTableView) {
                 CGFloat height = _produceTypeArr.count * 44;
                 if (height > SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) {
                     height = SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX;
                     _addMachineTableView.scrollEnabled = YES;
                 } else {
                     _addMachineTableView.scrollEnabled = NO;
                 }
                 _addMachineTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
             }
             [_addMachineTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _produceTypeArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.textColor = RGB(77, 77, 77);
    if (page == 0) {
        AddMachineModel *model = _produceTypeArr[indexPath.row];
        cell.textLabel.text = model.name;
    } else if (page == 1) {
        SelectProduceTypeModel *model = _produceTypeArr[indexPath.row];
        cell.textLabel.text = model.name;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (page == 0) {
        AddMachineModel *model = _produceTypeArr[indexPath.row];
        [self requestProduceProduct:model.ID];
    } else if (page == 1) {
        WEAKSELF;
        SelectProduceTypeModel *model = _produceTypeArr[indexPath.row];
        weakSelf.selectProduceTypeBlock(model);
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }
    page++;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
